import React from 'react';
import {inject, observer} from 'mobx-react';
import { withRouter } from 'react-router-dom';
import { BrowserHrmCondition } from 'weaver-mobile-page';
import { Tools, Bubble } from 'weaver-mobile';
import {toJS} from 'mobx';
import * as Util from "../../../util";
import dict from '../../../util/wea-workrelate-file';
const getLabel = Tools.getLabel;
dict.addWindowFunc();
@inject('goalShareStore','goalCommentStore')
@withRouter
@observer
class GoalShare extends React.Component {

    constructor(props){
        super(props);
        this.state = {};
        this.store = this.props.goalShareStore;
    }

    componentWillMount(){
        const { goalId } = this.props;
        let params = {
            workrelatetype: 1,
            bizid: goalId,
            operate: 'mobileSearch'
        }
        this.store.getShare(params)
    }
 
    componentWillReceiveProps(newProps){
   
    }


    componentWillUpdate(){
 
    }

    componentDidMount() {
    }

    componentWillUnmount () {
  
    }

    isDifferent = (oldArr,newArr) =>{
        if(newArr.length == 0){
            return false;
        }

        if(oldArr.length != newArr.length){
            return true;
        }

        let oldStr = "";
        for(let i = 0;i < oldArr.length;i ++){
            if(i == oldArr.length - 1){
                oldStr += oldArr[i].id 
            }else{
                oldStr += oldArr[i].id + ","
            }
        }
        let flag = newArr.every((item, index, array) =>{
           return oldStr.includes(oldStr.split(","),item.id)
        })
        return !flag;
    }

    render() { 
        const { goalId,canedit} = this.props;
        const { setState, shareStore, shareStoreValue } = this.store;
        return (
                 <div>
                    <BrowserHrmCondition
                    label={getLabel(19303, '人力资源条件')}
                    isSingle={false}
                    store={shareStore}
                    onChange={(...arg) => {
                        let oldArr = toJS(shareStoreValue);
                        this.isDifferent(oldArr,arg[2])
                        if(canedit){
                            setState({ shareStoreValue: arg[2] });
                            this.store.setShare({bizid:goalId})
                        }else if(!canedit && this.isDifferent(oldArr,arg[2])){
                            Bubble.error(getLabel(2012, "对不起，您暂时没有权限！"));
                        }
                    }}
                    viewAttr= {3}
                    valueArray={shareStoreValue}
                    />
                </div>
 
        )
    }
    
}
export default GoalShare; 